<template>
  <div class="auth-container">
    <div class="auth-card">
      <h2>注册</h2>
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label for="nickName">用户名</label>
          <input type="text" id="nickName" v-model="nickName" required />
        </div>
        <div class="form-group">
          <label for="mobile">手机号</label>
          <input type="mobile" id="mobile" v-model="mobile" required />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input type="password" id="password" v-model="password" required />
        </div>
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input type="password" id="confirmPassword" v-model="confirmPassword" required />
        </div>
        <button type="submit">注册</button>
      </form>
      <p class="auth-link">已经有账号了？<router-link to="/login">登录</router-link></p>
    </div>
  </div>
</template>

<script>
import { register } from '@/api/auth'; // 引入注册 API
import { ElMessage } from 'element-plus';

export default {
  data() {
    return {
      nickName: '',
      password: '',
      mobile: '',
      confirmPassword: '',
      loading: false, // 加载状态
      error: '', // 错误信息
    };
  },
  methods: {
    async handleRegister() {
      if (this.password !== this.confirmPassword) {
        this.showError('两次输入的密码不一致');
        this.error = '两次输入的密码不一致';
        return;
      }

      this.loading = true;
      this.error = '';

      try {
        const response = await register(this.nickName, this.password, this.mobile);
        console.log('注册成功:', response);

        // 注册成功后跳转到登录页面
        this.$router.push('/login');
      } catch (error) {
        this.error = error.message || '注册失败，请稍后重试';
      } finally {
        this.loading = false;
      }
    },
    showError(message) {
      ElMessage.error(message);
    },
  },
};
</script>

<style scoped>
.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  width: 100%;
}

.auth-card {
  background: rgba(255, 255, 255, 0.9);
  padding: 2rem;
  border-radius: 10px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
  width: 100%;
  max-width: 400px;
  text-align: center;
}

h2 {
  margin-bottom: 1.5rem;
  color: #333;
}

.form-group {
  margin-bottom: 1rem;
  text-align: left;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  color: #555;
}

input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 1rem;
  box-sizing: border-box;
}

button {
  width: 100%;
  padding: 0.75rem;
  background-color: #6a11cb;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: #2575fc;
}

.auth-link {
  margin-top: 1rem;
  color: #555;
}

.auth-link a {
  color: #6a11cb;
  text-decoration: none;
}

.auth-link a:hover {
  text-decoration: underline;
}

.error-message {
  color: red;
  margin-top: 1rem;
}
</style>